package cn.com.ghy.android.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.CompeteBrandInfoBean;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.CustomerInfoBeanParser;
import cn.com.ghy.android.bean.DataCollVisitComboxBean;
import cn.com.ghy.android.bean.DataCollVisitComboxBeanParser;
import cn.com.ghy.android.bean.DataCollectBaseInfoBean;
import cn.com.ghy.android.bean.DataCollectBaseInfoBeanParser;
import cn.com.ghy.android.bean.DataCollectCompeteInfoBean;
import cn.com.ghy.android.bean.DataCollectCompeteInfoBeanParser;
import cn.com.ghy.android.bean.DataCollectDayInfoBean;
import cn.com.ghy.android.bean.DataCollectDayInfoBeanParser;
import cn.com.ghy.android.bean.DataCollectInfoBean;
import cn.com.ghy.android.bean.DataCollectSaleInfoBean;
import cn.com.ghy.android.bean.DataCollectSaleInfoBeanParser;
import cn.com.ghy.android.bean.DisplaySiteBean;
import cn.com.ghy.android.bean.GoodsTypeBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.PictureTypeBean;
import cn.com.ghy.android.bean.SaleTypeBean;
import cn.com.ghy.android.bean.UnitInfoBean;
import cn.com.ghy.android.exception.HttpResponseNullException;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天拜访服务器
 * @author wuz  @date 2011-5-27 08:52:16
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class VisitOpeService {
	private String url = CommonStatic.URL_HEAD + "VisitOpe_Ar.asmx";
	private String methodName = "";
	private Map<String, String> params = null;

	private static final String USERID = "userid";
	private static final String PARTNER = "partner";
	private static final String WAYID = "wayid";
	private static final String VSDATE = "vsdate";
	private static final String PERNR = "pernr";
	private static final String STYPE = "stype";
	private static final String STRBASE = "strbase";
	private static final String STRDAY = "strday";
	private static final String STRPROMP = "strpromp";
	private static final String STRCMPT = "strcmpt";
	private static final String PICTYPE = "pictype";
	private static final String PIC = "pic";
	private static final String REMARK = "remark";

	public String GetTaskState(LoginInfoBean loginInfo) throws Exception {
		methodName = "GetTaskState";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		String result = null;
		try {
			result = HttpService.callWS(url, methodName, params);
		} catch (HttpResponseNullException hrne) {
			if (hrne.getMsg() == null) {
				throw new HttpResponseNullException();
			}
			result = "";
		}

		return result;
	}

	public List<CustomerInfoBean> GetTaskDetail(LoginInfoBean loginInfo) throws Exception {
		methodName = "GetTaskDetail";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CustomerInfoBean> list = CustomerInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public String SetVisitTime(LoginInfoBean loginInfo, String partner, String wayid,
			String vsdate, String stype) throws Exception {
		methodName = "SetVisitTime";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(PARTNER, partner);
		params.put(WAYID, wayid);
		params.put(VSDATE, vsdate);
		params.put(STYPE, stype);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public List<DataCollVisitComboxBean> GetVisitCombox() throws Exception {
		methodName = "GetVisitCombox";

		params = new HashMap<String, String>();

		String result = HttpService.callWS(url, methodName, params);
		if (null != result) {
			byte[] buffer = org.kobjects.base64.Base64.decode(result);
			byte[] info = CommonUtil.decompressByteArray(buffer);
			InputStream in = new ByteArrayInputStream(info);
			List<DataCollVisitComboxBean> list = DataCollVisitComboxBeanParser.parseXML(in);
			in.close();
			CommonUtil.checkListNull(list);
			return list;
		}
		return null;
	}

	public List<DataCollectBaseInfoBean> GetVisitBaseInfo(LoginInfoBean loginInfo, String partner,
			String wayid, String vsdate) throws Exception {
		methodName = "GetVisitBaseInfo";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(PARTNER, partner);
		params.put(WAYID, wayid);
		params.put(VSDATE, vsdate);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<DataCollectBaseInfoBean> list = DataCollectBaseInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}

	public List<DataCollectDayInfoBean> GetVisitDayInfo(LoginInfoBean loginInfo, String partner,
			String wayid, String vsdate) throws Exception {
		methodName = "GetVisitDayInfo";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(PARTNER, partner);
		params.put(WAYID, wayid);
		params.put(VSDATE, vsdate);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<DataCollectDayInfoBean> list = DataCollectDayInfoBeanParser.parseXML(in);
		in.close();

		CommonUtil.checkListNotNull(list);
		if (list.size() == 0) {
			list = GetLastBismt(loginInfo, partner);
		}
		return list;
	}

	public List<DataCollectDayInfoBean> GetLastBismt(LoginInfoBean loginInfo, String partner)
			throws Exception {
		methodName = "GetLastBismt";

		params = new HashMap<String, String>();
		params.put(PARTNER, partner);
		params.put(PERNR, loginInfo.getPernr());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<DataCollectDayInfoBean> list = DataCollectDayInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public List<DataCollectSaleInfoBean> GetVisitPromInfo(LoginInfoBean loginInfo, String partner,
			String wayid, String vsdate) throws Exception {
		methodName = "GetVisitPromInfo";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(PARTNER, partner);
		params.put(WAYID, wayid);
		params.put(VSDATE, vsdate);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<DataCollectSaleInfoBean> list = DataCollectSaleInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public List<DataCollectCompeteInfoBean> GetVisitCmptInfo(LoginInfoBean loginInfo,
			String partner, String wayid, String vsdate) throws Exception {
		methodName = "GetVisitCmptInfo";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(PARTNER, partner);
		params.put(WAYID, wayid);
		params.put(VSDATE, vsdate);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<DataCollectCompeteInfoBean> list = DataCollectCompeteInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public DataCollectInfoBean getVisitDataCollAll(LoginInfoBean loginInfo, String partner,
			String wayid, String vsdate) throws Exception {
		DataCollectInfoBean bean = new DataCollectInfoBean();
		List<DataCollVisitComboxBean> dataCollVisitComboxList = GetVisitCombox();

		List<UnitInfoBean> unitInfoList = dataCollVisitComboxList.get(0).getUnitInfoList();
		CommonUtil.checkListNull(unitInfoList);

		List<CompeteBrandInfoBean> competeBrandInfoList = dataCollVisitComboxList.get(0)
				.getCompeteBrandInfoList();
		CommonUtil.checkListNull(competeBrandInfoList);

		List<SaleTypeBean> saleTypeList = dataCollVisitComboxList.get(0).getSaleTypeList();
		CommonUtil.checkListNull(saleTypeList);

		List<DisplaySiteBean> displaySiteList = dataCollVisitComboxList.get(0).getDisplaySiteList();
		CommonUtil.checkListNull(displaySiteList);

		List<PictureTypeBean> pictureTypeList = dataCollVisitComboxList.get(0).getPictureTypeList();
		CommonUtil.checkListNull(pictureTypeList);

		List<GoodsTypeBean> goodsTypeList = dataCollVisitComboxList.get(0).getGoodsType();
		CommonUtil.checkListNull(goodsTypeList);
		bean.setDataCollVisitComboxList(dataCollVisitComboxList);

		List<DataCollectBaseInfoBean> dataCollectBaseInfoList = GetVisitBaseInfo(loginInfo,
				partner, wayid, vsdate);
		bean.setDataCollectBaseInfoList(dataCollectBaseInfoList);

		List<DataCollectDayInfoBean> dataCollectDayInfoList = GetVisitDayInfo(loginInfo, partner,
				wayid, vsdate);
		bean.setDataCollectDayInfoList(dataCollectDayInfoList);

		List<DataCollectSaleInfoBean> dataCollectSaleInfoList = GetVisitPromInfo(loginInfo,
				partner, wayid, vsdate);
		bean.setDataCollectSaleInfoList(dataCollectSaleInfoList);

		List<DataCollectCompeteInfoBean> dataCollectCompeteInfoList = GetVisitCmptInfo(loginInfo,
				partner, wayid, vsdate);
		bean.setDataCollectCompeteInfoList(dataCollectCompeteInfoList);

		return bean;
	}

	public String SetVisitInfo(LoginInfoBean loginInfo,
			DataCollectBaseInfoBean dataCollectBaseInfo,
			List<DataCollectDayInfoBean> dataCollectDayInfoList,
			List<DataCollectSaleInfoBean> dataCollectSaleInfoList,
			List<DataCollectCompeteInfoBean> dataCollectCompeteInfoList) throws Exception {
		String methodName = "SetVisitInfo";
		String strbaseXML = "";
		String strdayXML = "";
		String strprompXML = "";
		String strcmptXML = "";

		if (null == dataCollectBaseInfo) {
			return "处理基本信息出错,请联系管理员!";
		}

		if (null == dataCollectDayInfoList) {
			return "处理日常信息出错,请联系管理员!";
		}

		if (null == dataCollectSaleInfoList) {
			return "处理促销信息出错,请联系管理员!";
		}

		if (null == dataCollectCompeteInfoList) {
			return "处理竞品信息出错,请联系管理员!";
		}

		StringWriter writer = new StringWriter();
		strbaseXML = DataCollectBaseInfoBeanParser
				.createXML(loginInfo, dataCollectBaseInfo, writer);
		writer.close();
		strbaseXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(strbaseXML
				.getBytes()));

		writer = new StringWriter();
		strdayXML = DataCollectDayInfoBeanParser.createXML(loginInfo, dataCollectBaseInfo,
				dataCollectDayInfoList, writer);
		writer.close();
		strdayXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(strdayXML
				.getBytes()));

		writer = new StringWriter();
		strprompXML = DataCollectSaleInfoBeanParser.createXML(loginInfo, dataCollectBaseInfo,
				dataCollectSaleInfoList, writer);
		writer.close();
		strprompXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(strprompXML
				.getBytes()));

		writer = new StringWriter();
		strcmptXML = DataCollectCompeteInfoBeanParser.createXML(loginInfo, dataCollectBaseInfo,
				dataCollectCompeteInfoList, writer);
		writer.close();
		strcmptXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(strcmptXML
				.getBytes()));

		params = new HashMap<String, String>();
		params.put(STRBASE, strbaseXML);
		params.put(STRDAY, strdayXML);
		params.put(STRPROMP, strprompXML);
		params.put(STRCMPT, strcmptXML);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public String SetVisitPic(LoginInfoBean loginInfo, DataCollectBaseInfoBean dataCollectBaseInfo,
			String vsdate, String pictype, String pic, String remark) throws Exception {
		String methodName = "SetVisitPic";
		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(PARTNER, dataCollectBaseInfo.getCustomerInfo().getPartner());
		params.put(WAYID, dataCollectBaseInfo.getMyWay().getWayid());
		params.put(VSDATE, vsdate);
		params.put(PICTYPE, pictype);
		params.put(PIC, pic);
		params.put(REMARK, remark);
		String result = HttpService.callWS(url, methodName, params);
		return result;
	}
}
